<?php

namespace app\sisome\controller;
use think\Controller;
use think\Request;
use think\Db;
class Project extends Common
{	
	// 添加矿机11222
	public function addproject()
	{		//KM++矿机添加-display
		$request = Request::instance();
		if($request->isPost()){
			$data = $this->saveproject();
			$data['name'] = input('name');
			$data['price'] = input('price');
			$data['stock'] = input('stock');
			$data['capacity'] = input('capacity');
			$data['limit'] = input('limit');
			$data['to_open_up_time'] = strtotime(input('to_open_up_time'));
			$data['content'] = input('content');
			$data['kjsl'] = input('kjsl');
			$data['yxzq'] = input('yxzq');

			if (db('shop_project')->insert($data)) {
				$this->success('添加成功!','project/listproject');
			} else {
				$this->success('添加失败！');
			}
		}

		return $this->fetch();
	}
	public function saveproject()
	{		//KM++矿机添加提交

		$file = request()->file('file');

		if ($file) {
			// 移动到框架应用根目录/public/uploads/Pic 目录下
			$info = $file->validate(['size' => 3145728])->move(ROOT_PATH . 'public' . DS . 'uploads/Pic/');
			if ($info) {
				// 成功上传后 获取上传信息
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$data['imagepath'] = '/Uploads/Pic/' . $info->getSaveName();
			} else {
				// 上传失败获取错误信息
				return $this->error($file->getError());
			}
		}
		$data['addtime'] = time();
		$data['zt'] = 0;
		return $data;
	}
	public function listproject()
	{		//KM++矿机列表
		$re = db('shop_project')->field(true)->paginate(12);
		
		$this->assign('lists', $re);
		return view();
	}
	public function delProject()
	{		//KM++删除矿机
		$id = input('id');
		$re = db('shop_project')->where(array('id' => $id))->delete();
		$this->redirect('listproject');
	}
	public function project()
	{			//KM++矿机修改-display
		$id = input('id');
		$arr = db('shop_project')->where(array('id' => $id))->find();
		$this->assign('ssid', session_id());
		$this->assign('arr', $arr);
		return view();
	}
	public function updateProject()
	{	//KM++矿机修改-提交
		$id = input('id');
		$data = $this->saveproject();
		$data['name'] = input('name');
		$data['price'] = input('price');
		$data['stock'] = input('stock');
		$data['capacity'] = input('capacity');
		$data['limit'] = input('limit');
		$data['to_open_up_time'] = strtotime(input('to_open_up_time'));
		$data['content'] = input('content');
		$data['kjsl'] = input('kjsl');
		$data['yxzq'] = input('yxzq');
		if (db('shop_project')->where(array('id' => $id))->update($data)) {
			$this->success('修改成功!','project/listproject');
		} else {
			$this->error('修改失败！');
		}
	}
	public function ztProject()
	{		//KM++修改矿机状态 上架/下架
		$id = input('id');
		$zt = input('zt');
		$re = db('shop_project')->where(array('id' => $id))->update(array('zt' => $zt));
		$this->redirect('listproject');
	}
	public function ztProject1()
	{		//KM++修改矿机状态 推荐/不推荐
		$id = input('id');
		$zt1 = input('zt1');
		$re = db('shop_project')->where(array('id' => $id))->update(array('zt1' => $zt1));
		$this->redirect('listproject');
	}


	
	function toggleShop()
	{	//修改系统状态
		if (IS_POST) {
			$order = I('post.toggle');
			if ($order == 1) {
				// $re = db('system')->where(['SYS_ID'=>'1'])->setField('toggleshop',1);
				$re = db('system')->where(array('SYS_ID' => 1))->setField('toggleshop', 1);
			} elseif ($order == 0) {
				// $re = db('system')->where(['SYS_ID'=>'1'])->setField('toggleshop',0);
				$re = db('system')->where(array('SYS_ID' => '1'))->setField('toggleshop', 0);
			} else {
				die('非法操作！');
			}
			if ($re) {
				$this->success('设置成功！');
			} else {
				$this->error('设置失败！');
			}
		}
	}

	// 矿机订单列表
	public function listorderform()
	{	
		$map = array();
		if (input('val')) {
			$key = input('key');
			$map[$key] = input('val');
		}

		$re = db('shop_orderform')->where($map)->order('id desc')->paginate(12,false,['query'=>Request::instance()->param()]);
		$this->assign('lists', $re);
		//$this->assign('param', $request->param());
		return view();
	}

	// 扣回订单
	public function del_order()
	{
		$id = input('id');

		$shop_orderform = db('shop_orderform')->where('id',$id)->find();
		if($shop_orderform['starttime']){
			db('user')->where('uname',$shop_orderform['mobile'])->setDec('power',$shop_orderform['kjsl']);
			db('userget')->where('kcid',$id)->where('earnings',1)->delete();
		}
		db('shop_orderform')->where('id',$id)->delete();

		$this->success('扣回成功');
	}


}
